package com.xpgk.mvc.statistic.yearSales;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.statistic.daySales.DaySalesStatisticSQL;
import com.xpgk.tools.ToolDateTime;

@Service(name = YearSalesStatisticService.serviceName)
public class YearSalesStatisticService extends  BaseService{
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(YearSalesStatisticService.class);
	public static final String serviceName = "yearSalesStatisticService";
	
	public  List<Record>  getView(String date,String organIds)
	{
		List<Record> result = new ArrayList<>();
		if(!"-1".equals(date) && !"-1".equals(organIds)){
			int hjpeopleNum=0;//合计新增人数
			int hjlcs=0;//合计疗程数
			int hjdlcrs=0;//合计单疗程人数
			double hjdlcje=0;//合计单疗程金额
			int hjbzlcrs=0;//合计标准疗程人数
			int hjbzlclcs=0;//合计标准疗程疗程数
			double hjbzlcje=0;//合计标准疗程金额
			int hjzlcrs=0;//合计转疗程人数
			int hjzlclcs=0;//合计转疗程疗程数
			double hjzlcje=0;//合计转疗程金额
			double hjmrjzje=0;//合计每月减重金额
			double hjmrproje=0;//合计每月产品金额
			//查询其他产品
			String sql_product = getSqlMy(DaySalesStatisticSQL.sqlId_product);
			List<Record> products = Db.find(sql_product, Code.lc_ids, Code.proYt_ids);
			
			List<String> monthes = ToolDateTime.getYearAllMonthes(date);
			if(monthes.size() > 0){
				
				for(String month : monthes){
					Record r = new Record();
					
					double lctotalprice=0;//每月减重金额
					double producttotalprice=0;//每月产品金额
					//日期
					r.set("date", month);
					
					//新增人数
					String sql_newPeopleNum = getSqlMy(YearSalesStatisticSQL.sqlId_newPeopleNum);
					Record newPeopleNum = Db.findFirst(sql_newPeopleNum, month, Code.lc_ids, organIds, month);
					if(null != newPeopleNum){
						r.set("newPeopleNum", newPeopleNum.getLong("newPeopleNum"));
						hjpeopleNum+=newPeopleNum.getLong("newPeopleNum")==null?0:newPeopleNum.getLong("newPeopleNum");
					}
					
					//疗程数
					String sql_newLcNum = getSqlMy(YearSalesStatisticSQL.sqlId_newLcNum);
					Record newLcNum = Db.findFirst(sql_newLcNum, month, Code.lc_ids, Code.bzlc_units, organIds, month);
					if(null != newLcNum){
						r.set("newLcNum", newLcNum.getBigDecimal("newLcNum"));
						hjlcs+=newLcNum.getBigDecimal("newLcNum")==null?0:Integer.parseInt(newLcNum.getBigDecimal("newLcNum").toString());
					}
					
					//单疗程
					String sql_singleLc = getSqlMy(YearSalesStatisticSQL.sqlId_lc);
					Record singleLc = Db.findFirst(sql_singleLc, month, Code.lc_ids, Code.dlc_units, organIds, month);
					if(null != singleLc){
						r.set("singleLc_peopleNum", singleLc.getLong("peopleNum"));
						r.set("singleLc_unitPrice", singleLc.getFloat("unitPrice"));
						r.set("singleLc_totalPrice", singleLc.getDouble("totalPrice"));
						lctotalprice+=singleLc.getDouble("totalPrice")==null?0:singleLc.getDouble("totalPrice");
						hjdlcrs+=singleLc.getLong("peopleNum")==null?0:singleLc.getLong("peopleNum");
						hjdlcje+=singleLc.getDouble("totalPrice")==null?0:singleLc.getDouble("totalPrice");
					}
					
					//标准疗程
					String sql_normLc = getSqlMy(YearSalesStatisticSQL.sqlId_lc);
					Record normLc = Db.findFirst(sql_normLc, month, Code.lc_ids, Code.bzlc_units, organIds, month);
					if(null != normLc){
						r.set("normLc_peopleNum", normLc.getLong("peopleNum"));
						r.set("normLc_lcNum", normLc.getBigDecimal("lcNum"));
						r.set("normLc_unitPrice", normLc.getFloat("unitPrice"));
						r.set("normLc_totalPrice", normLc.getDouble("totalPrice"));
						lctotalprice+=normLc.getDouble("totalPrice")==null?0:normLc.getDouble("totalPrice");
						hjbzlcrs+=normLc.getLong("peopleNum")==null?0:normLc.getLong("peopleNum");
						hjbzlclcs+=normLc.getBigDecimal("lcNum")==null?0:Integer.parseInt(normLc.getBigDecimal("lcNum").toString());
						hjbzlcje+=normLc.getDouble("totalPrice")==null?0:normLc.getDouble("totalPrice");
					}
					
					//转疗程
					String sql_upLc = getSqlMy(YearSalesStatisticSQL.sqlId_upLc);
					Record upLc = Db.findFirst(sql_upLc, month, Code.lc_ids, organIds, month);
					if(null != upLc){
						r.set("upLc_peopleNum", upLc.getLong("peopleNum"));
						r.set("upLc_lcNum", upLc.getBigDecimal("lcNum"));
						r.set("upLc_totalPrice", upLc.getDouble("totalPrice"));
						lctotalprice+=upLc.getDouble("totalPrice")==null?0:upLc.getDouble("totalPrice");
						hjzlcrs+=upLc.getLong("peopleNum")==null?0:upLc.getLong("peopleNum");
						hjzlclcs+=upLc.getBigDecimal("lcNum")==null?0:Integer.parseInt(upLc.getBigDecimal("lcNum").toString());
						hjzlcje+=upLc.getDouble("totalPrice")==null?0:upLc.getDouble("totalPrice");
					}
					
					r.set("lctotalprice", lctotalprice);
					//其他产品
					
					if(products.size() > 0){
//						Double sum = 0.0;
						for(Record product : products){
							String sql_productStatistic = getSqlMy(YearSalesStatisticSQL.sqlId_productStatistic);
							Record productStatistic = Db.findFirst(sql_productStatistic, month, product.getStr("ids"), organIds, month);
							if(null != productStatistic){
								r.set(product.getStr("bh") + "_num", productStatistic.getBigDecimal("num"));
								r.set(product.getStr("bh") + "_price", productStatistic.getDouble("price"));
								producttotalprice += productStatistic.getDouble("price");
							}
						}
						r.set("producttotalprice", producttotalprice);
					}
					hjmrjzje+=lctotalprice;
					hjmrproje+=producttotalprice;
					r.set("sum", lctotalprice+producttotalprice);
					result.add(r);
					
					String curDate = ToolDateTime.format(new Date(), ToolDateTime.pattern_ym);
					if(month.equals(curDate)){
						break;
					}
				}
				Record  hj=new Record();
				hj.set("date", "合计");
				hj.set("newpeoplenum", hjpeopleNum);
				hj.set("newlcnum", hjlcs);
				hj.set("singleLc_peopleNum", hjdlcrs);
				hj.set("singleLc_totalPrice", hjdlcje);
				hj.set("normLc_peopleNum", hjbzlcrs);
				hj.set("normLc_lcNum", hjbzlclcs);
				hj.set("normLc_totalPrice", hjbzlcje);
				hj.set("upLc_peopleNum", hjzlcrs);
				hj.set("upLc_lcNum", hjzlclcs);
				hj.set("upLc_totalPrice", hjzlcje);
				hj.set("lctotalprice", hjmrjzje);
				for(Record product:products)
				{	
					double  hjprosl=0;
					double  hjproje=0;
					for(int n=0;n<result.size();n++)
					{
						hjprosl+=result.get(n).getBigDecimal(product.getStr("bh") + "_num")==null?0:Double.parseDouble(result.get(n).getBigDecimal(product.getStr("bh") + "_num").toString());
						hjproje+=result.get(n).getDouble(product.getStr("bh") + "_price")==null?0:result.get(n).getDouble(product.getStr("bh") + "_price");
					}
					hj.set(product.getStr("bh") + "_num", hjprosl);
					hj.set(product.getStr("bh") + "_price", hjproje);
				}
				hj.set("producttotalprice", hjmrproje);
				hj.set("sum", hjmrjzje+hjmrproje);
				result.add(hj);
			}
		}
		return  result;
	}
	
	
	

}
